package com.changyinBus.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.changyinBus.common.annotation.Log;
import com.changyinBus.common.core.controller.BaseController;
import com.changyinBus.common.core.domain.AjaxResult;
import com.changyinBus.common.core.domain.entity.SysArticle;
import com.changyinBus.common.core.domain.entity.SysCate;
import com.changyinBus.common.core.page.TableDataInfo;
import com.changyinBus.common.enums.BusinessType;
import com.changyinBus.common.utils.IDGenerator;
import com.changyinBus.system.service.ISysArticleService;
import com.changyinBus.system.service.ISysCateService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system/article")
public class SysArticleController extends BaseController {
    @Resource
    private ISysArticleService sysArticleService;
    @Resource
    private ISysCateService sysCateService;

    /**
     * 获取新闻资讯列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysArticle article) {
        startPage();
        List<SysArticle> list = sysArticleService.selectArticleList(article);
        return getDataTable(list);
    }

    /**
     * 根据新闻资讯编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = { "/", "/{articleId}" })
    public AjaxResult getInfo(@PathVariable(value = "articleId", required = false) Long articleId) {
        return success(sysArticleService.selectArticleById(articleId));
    }

    @GetMapping(value = { "/getInfoByType" })
    public AjaxResult getInfoByArticleType(int articleType, int cateId, Long tenantId) {
        SysArticle article = sysArticleService.selectArticleByArticleType(articleType, cateId, tenantId);
        if(article == null){
            article = new SysArticle();
        }
        return success(article);
    }

    /**
     * 新增新闻资讯
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "新闻资讯管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysArticle article) {
        article.setArticleId(IDGenerator.getID());
        article.setCreateBy(getUsername());
        return success(sysArticleService.insertArticle(article));
    }

    /**
     * 修改新闻资讯
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "新闻资讯管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysArticle article) {
        article.setUpdateBy(getUsername());
        return success(sysArticleService.updateArticle(article));
    }

    @PostMapping(value = { "/editArticle" })
    public AjaxResult editArticle(@Validated @RequestBody SysArticle temp) {
        SysArticle article = sysArticleService.selectArticleByArticleType(temp.getArticleType(), temp.getCateId(), temp.getTenantId());
        int result = 0;
        if(article == null){
            article = new SysArticle(temp);
            article.setCreateBy(getUsername());
            result = sysArticleService.insertArticle(article);
        }else{
            article.setParam(temp);
            article.setUpdateBy(getUsername());
            result = sysArticleService.updateArticle(article);
        }
        return success(result);
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "新闻资讯管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysArticle article) {
        article.setUpdateBy(getUsername());
        return success(sysArticleService.updateArticleStatus(article));
    }

    /**
     * 删除新闻资讯
     */
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "新闻资讯管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleId}")
    public AjaxResult remove(@PathVariable Long articleId) {
        return success(sysArticleService.deleteArticleById(articleId));
    }

    /**
     * 获取分类树列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/cateTree")
    public AjaxResult cateTree(SysCate cate) {
        return success(sysCateService.selectCateTreeList(cate));
    }

}
